package org.kaleido.client.view.impl;

import org.kaleido.client.ClientFactory;
import org.kaleido.client.resources.KaleidoResources;
import org.kaleido.client.view.ApplicationLayoutView;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class ApplicationLayoutImpl implements ApplicationLayoutView {

    private static final int MENU_HEIGHT_PX = 50;

    private static final int SEARCH_HEIGHT_PX = 20;

    private static final int INFO_LOG_PANEL_HEIGHT_PC = 20;

    private static final int LEFT_PANEL_WITH_PX = 80;

    /**
     * Layout de la aplicación que contiene los otros paneles.
     */
    private final LayoutPanel applicationLayoutPanel = new LayoutPanel();

    /**
     * Layout de la aplicación que contiene los otros paneles.
     */
    private final LayoutPanel mainLayoutPanel = new LayoutPanel();

    /**
     * Layout de la aplicación que contiene los otros paneles.
     */
    private final LayoutPanel menuLayoutPanel = new LayoutPanel();

    /**
     * Layout de la aplicación que contiene los otros paneles.
     */
    private final LayoutPanel dataLayoutPanel = new LayoutPanel();

    /**
     * Layout de la aplicación que contiene los otros paneles.
     */
    private final LayoutPanel dataLogLayoutPanel = new LayoutPanel();

    /**
     * Panel asociado a la cabecera.
     */
    private HeaderMenuViewImpl headerMenuPanel = new HeaderMenuViewImpl();

    /**
     * Panel asociado al menu.
     */
    private SimplePanel leftMenuPanel = new SimplePanel();

    /**
     * Panel asociado al listado con la informacion de los logs.
     */
    private SimplePanel dataPanel = new SimplePanel();

    /**
     * Panel asociado al muro.
     */
    private SimplePanel infoLogPanel = new SimplePanel();

    /**
     * Panel asociado al muro.
     */
    private SimplePanel searchPanel = new SimplePanel();

    /**
     * Contructor.
     */
    public ApplicationLayoutImpl(ClientFactory clientFactory, RootLayoutPanel parentView) {
        initializeMembersPannels();
        setDefaultLayout();
        parentView.add(this.applicationLayoutPanel);
    }

    private void initializeMembersPannels() {
        // Inicializamos los paneles
        dataPanel.setStyleName(KaleidoResources.INSTANCE.css().dataPanel());
        headerMenuPanel.setStyleName(KaleidoResources.INSTANCE.css().headerMenuPanel());
        leftMenuPanel.setStyleName(KaleidoResources.INSTANCE.css().leftMenuPanel());
        infoLogPanel.setStyleName(KaleidoResources.INSTANCE.css().infoLogPanel());
        searchPanel.setStyleName(KaleidoResources.INSTANCE.css().searchPanel());

        // headerMenuPanel.add(new HeaderMenuViewImpl());
        leftMenuPanel.add(new ListLogMenuViewImpl());
        dataPanel.add(new ListLogDataViewImpl());
        infoLogPanel.add(new InfoLogViewImpl());
        searchPanel.add(new SearchViewImpl());

        // Añadimos los paneles al layout
        applicationLayoutPanel.add(headerMenuPanel);
        applicationLayoutPanel.add(mainLayoutPanel);
        mainLayoutPanel.add(menuLayoutPanel);
        mainLayoutPanel.add(dataLayoutPanel);
        menuLayoutPanel.add(leftMenuPanel);
        dataLayoutPanel.add(searchPanel);
        dataLayoutPanel.add(dataLogLayoutPanel);
        dataLogLayoutPanel.add(dataPanel);
        dataLogLayoutPanel.add(infoLogPanel);
    }

    @Override
    public LayoutPanel getMainLayoutPanel() {
        return mainLayoutPanel;
    }

    public void setDefaultLayout() {
        // Establecemos las alturas
        initializeMainLayout();
        dataLogLayoutPanel.setWidgetTopBottom(dataPanel, 0, Unit.PX, 0, Unit.PX);
        infoLogPanel.setVisible(false);
        // animamos la transicion
        mainLayoutPanel.animate(500);
    }

    /**
     * TODO
     */
    public void setInfoLogLayout() {
        // Establecemos las alturas
        initializeMainLayout();
        dataLogLayoutPanel.setWidgetTopHeight(dataPanel, 0, Unit.PX, 100 - INFO_LOG_PANEL_HEIGHT_PC, Unit.PCT);
        dataLogLayoutPanel.setWidgetTopBottom(dataPanel, INFO_LOG_PANEL_HEIGHT_PC, Unit.PCT, 0, Unit.PX);

        infoLogPanel.setVisible(true);
        // animamos la transicion
        mainLayoutPanel.animate(500);
    }

    public void setAdminMenuLayout() {
        // Establecemos las alturas
        mainLayoutPanel.setWidgetTopBottom(dataLayoutPanel, 0, Unit.PX, 0, Unit.PX);
        menuLayoutPanel.setVisible(false);
        // animamos la transicion
        mainLayoutPanel.animate(500);
    }

    private void initializeMainLayout() {
        applicationLayoutPanel.setWidgetTopHeight(headerMenuPanel, 0, Unit.PX, MENU_HEIGHT_PX, Unit.PX);
        applicationLayoutPanel.setWidgetTopBottom(mainLayoutPanel, MENU_HEIGHT_PX, Unit.PX, 0, Unit.PX);
        mainLayoutPanel.setWidgetLeftWidth(menuLayoutPanel, 0, Unit.PX, LEFT_PANEL_WITH_PX, Unit.PX);
        mainLayoutPanel.setWidgetLeftRight(dataLayoutPanel, LEFT_PANEL_WITH_PX, Unit.PX, 0, Unit.PX);
        dataLayoutPanel.setWidgetTopHeight(searchPanel, 0, Unit.PX, SEARCH_HEIGHT_PX, Unit.PX);
        dataLayoutPanel.setWidgetTopBottom(dataLogLayoutPanel, SEARCH_HEIGHT_PX, Unit.PX, 0, Unit.PX);
        menuLayoutPanel.setVisible(true);
    }

    @Override
    public AcceptsOneWidget getHeaderMenuContainer() {
        return new AcceptsOneWidget() {
            @Override
            public void setWidget(IsWidget w) {
                Widget widget = Widget.asWidgetOrNull(w);
                headerMenuPanel.add(widget);
            }
        };
    }

    @Override
    public AcceptsOneWidget getLeftMenuContainer() {
        return new AcceptsOneWidget() {
            @Override
            public void setWidget(IsWidget w) {
                Widget widget = Widget.asWidgetOrNull(w);
                leftMenuPanel.setWidget(widget);
            }
        };
    }

    @Override
    public AcceptsOneWidget getSearchContainer() {
        return new AcceptsOneWidget() {
            @Override
            public void setWidget(IsWidget w) {
                Widget widget = Widget.asWidgetOrNull(w);
                searchPanel.setWidget(widget);
            }
        };
    }

    @Override
    public AcceptsOneWidget getInfoLogContainer() {
        return new AcceptsOneWidget() {
            @Override
            public void setWidget(IsWidget w) {
                Widget widget = Widget.asWidgetOrNull(w);
                headerMenuPanel.setWidget(widget);
            }
        };
    }

    @Override
    public AcceptsOneWidget getDataLogContainer() {
        return new AcceptsOneWidget() {
            @Override
            public void setWidget(IsWidget w) {
                Widget widget = Widget.asWidgetOrNull(w);
                dataPanel.setWidget(widget);
            }
        };
    }
}